package com.example.order.controller;

import com.example.model.dto.Order;
import com.example.model.dto.Product;
import com.example.model.vo.KDAppToken;
import com.example.order.feign.KDFeignClient;
import com.example.order.feign.ProductFeignClient;
import com.example.order.properties.OrderProperties;
import com.example.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
 * @Author: ft
 * @Date:2025/8/19
 * @Description: 相关单据：
 */
@RestController
@RequestMapping("/order")
//@RefreshScope  //自动刷新配置
/**
 * 读取nacos并自动刷新配置
 *  1.在controller加上@RefreshScope注解
 *  @Value("${order.timeout}")
 *  String ordertimeout;
 *
 *  2.添加properties类
 *    @Data
 * @Component
 * @ConfigurationProperties(prefix = "order") //配置批量绑定，不再需要@RefreshScope注解就能自动刷新
 * public class OrderProperties {
 *
 *     private String timeout; //订单超时时间
 *
 *     private String autoMation; //订单自动化处理开关
 * }
 */
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    OrderProperties orderProperties;

    @Autowired
    ProductFeignClient productFeignClient;

    @Autowired
    KDFeignClient kdFeignClient;

    @Autowired
    OrderService orderService;

//    @Value("${order.timeout}")
//    String ordertimeout;

    @GetMapping("/config")
    public String config() {
        return orderProperties.toString();
    }


    @PostMapping("/create")
    public Order createOrder(@RequestParam String orderId, @RequestParam String productId) {
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String host = choose.getHost();
        int port = choose.getPort();
        System.out.println("service-product的host:" + host + ",port:" + port);
        Product forObject = restTemplate.getForObject("http://" + host + ":" + port + "/product/get/" + productId, Product.class);

        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderAmount(null);
        order.setProducts(Arrays.asList(forObject));
        return order;

    }


    /**
     * 注解方式实现负载均衡调用  bean注入RestTemplate的时候加上@LoadBalanced注解,然后调用的url用服务名代替具体的host和port
     *
     * @param orderId
     * @param productId
     * @return
     */
    @PostMapping("/create2")
    public Order createOrder2(@RequestParam String orderId, @RequestParam String productId) {
//        ServiceInstance choose = loadBalancerClient.choose("service-product");
//        String host = choose.getHost();
//        int port = choose.getPort();
//        System.out.println("service-product的host:" + host + ",port:" + port);
        Product forObject = restTemplate.getForObject("http://service-product/product/get/" + productId, Product.class);

        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderAmount(null);
        order.setProducts(Arrays.asList(forObject));
        return order;

    }


    /**
     * 声明式调用其他微服务
     *
     * @param orderId
     * @param productId
     * @return
     */
    @PostMapping("/create3")
    public Order createOrder3(@RequestParam String orderId, @RequestParam String productId) {
//        ServiceInstance choose = loadBalancerClient.choose("service-product");
//        String host = choose.getHost();
//        int port = choose.getPort();
//        System.out.println("service-product的host:" + host + ",port:" + port);
        return orderService.createOrder(orderId, productId);

    }


    /**
     * openfeign方式调用外部api
     *
     * @return
     */
    @PostMapping("/testapi")
    public String testapi() {
//        String appToken = kdFeignClient.getAppToken("projectcost", "GetProjectcostApi1;tttttt2024", "ierp",
//                "1419591444322059264", "zh_CN");
        String appToken = kdFeignClient.getAppToken(new KDAppToken("projectcost", "GetProjectcostApi1;tttttt2024",
                "ierp", "1419591444322059264", "zh_CN"));
        return appToken;

    }
}